/*
 * Agenda.java
 *
 * Created on 05/10/2010, 14:41:21
 * Copyright Fm
 */
package Soligee.view;

import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeBuscarImplements;
import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeCadastrarImplements;
import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeRemoverImplements;
import Soligee.model.vo.cadastros.ContaVO;
import Soligee.model.vo.cadastros.LembreteVO;
import Soligee.model.vo.cadastros.ParcelaNFEntradaVO;
import Soligee.model.vo.cadastros.ParcelaVendaVO;
import com.icesoft.faces.component.ext.HtmlDataTable;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlInputTextarea;
import com.icesoft.faces.component.jsfcl.data.DefaultTableDataModel;
import com.icesoft.faces.component.jsfcl.data.SelectInputDateBean;
import com.icesoft.faces.component.panellayout.PanelLayout;
import com.icesoft.faces.component.panelpopup.PanelPopup;
import com.icesoft.faces.component.selectinputdate.SelectInputDate;
import com.sun.rave.faces.converter.SqlDateConverter;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.event.ValueChangeEvent;


/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 */
public class Agenda extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
    private int __placeholder;
    private SessionFacadeBuscarImplements facadeBusca = new SessionFacadeBuscarImplements();
    private SessionFacadeCadastrarImplements facadeCadastro = new SessionFacadeCadastrarImplements();
    private SessionFacadeRemoverImplements facadeRemover = new SessionFacadeRemoverImplements();
    private List<ParcelaVendaVO> listaDeParcelas;
    private List<LembreteVO> listaDeLembretes;
    private List<ParcelaNFEntradaVO> listaDeContasNf;
    private List<ContaVO> listaDeContas;

    public List<ParcelaNFEntradaVO> getListaDeContasNf() {
        return listaDeContasNf;
    }

    public void setListaDeContasNf(List<ParcelaNFEntradaVO> listaDeContasNf) {
        this.listaDeContasNf = listaDeContasNf;
    }

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
   private SelectInputDateBean selectInputDate1Bean = new SelectInputDateBean();

   public SelectInputDateBean getSelectInputDate1Bean() {
      return selectInputDate1Bean;
   }

   public void setSelectInputDate1Bean(SelectInputDateBean sidb) {
      this.selectInputDate1Bean = sidb;
   }
   
   
   private DefaultTableDataModel dataTable1Model1 = new DefaultTableDataModel();

   public DefaultTableDataModel getDataTable1Model1() {
      return dataTable1Model1;
   }

   public void setDataTable1Model1(DefaultTableDataModel dtdm) {
      this.dataTable1Model1 = dtdm;
   }
   private DefaultTableDataModel dataTable2Model1 = new DefaultTableDataModel();

   public DefaultTableDataModel getDataTable2Model1() {
      return dataTable2Model1;
   }

   public void setDataTable2Model1(DefaultTableDataModel dtdm) {
      this.dataTable2Model1 = dtdm;
   }

   private DefaultTableDataModel dataTable3Model1 = new DefaultTableDataModel();

   public DefaultTableDataModel getDataTable3Model1() {
      return dataTable3Model1;
   }

   public void setDataTable3Model1(DefaultTableDataModel dtdm) {
      this.dataTable3Model1 = dtdm;
   }

   private DefaultTableDataModel dataTable4Model1 = new DefaultTableDataModel();

   public DefaultTableDataModel getDataTable4Model1() {
      return dataTable4Model1;
   }

   public void setDataTable4Model1(DefaultTableDataModel dtdm) {
      this.dataTable4Model1 = dtdm;
   }

    private HtmlDataTable tabelaContasAReceber = new HtmlDataTable();

    public HtmlDataTable getTabelaContasAReceber() {
        return tabelaContasAReceber;
    }

    public void setTabelaContasAReceber(HtmlDataTable hdt) {
        this.tabelaContasAReceber = hdt;
    }
    private HtmlDataTable tabelaLembrete = new HtmlDataTable();

    public HtmlDataTable getTabelaLembrete() {
        return tabelaLembrete;
    }

    public void setTabelaLembrete(HtmlDataTable hdt) {
        this.tabelaLembrete = hdt;
    }
    private PanelLayout panelPainelBusca = new PanelLayout();

    public PanelLayout getPanelPainelBusca() {
        return panelPainelBusca;
    }

    public void setPanelPainelBusca(PanelLayout pl) {
        this.panelPainelBusca = pl;
    }
    private SelectInputDate campoDataEscolha = new SelectInputDate();

    public SelectInputDate getCampoDataEscolha() {
        return campoDataEscolha;
    }

    public void setCampoDataEscolha(SelectInputDate sid) {
        this.campoDataEscolha = sid;
    }
    private SqlDateConverter sqlDateConverter1 = new SqlDateConverter();

    public SqlDateConverter getSqlDateConverter1() {
        return sqlDateConverter1;
    }

    public void setSqlDateConverter1(SqlDateConverter sdc) {
        this.sqlDateConverter1 = sdc;
    }
    private SelectInputDateBean selectInputDateBean1 = new SelectInputDateBean();

    public SelectInputDateBean getSelectInputDateBean1() {
        return selectInputDateBean1;
    }

    public void setSelectInputDateBean1(SelectInputDateBean sidb) {
        this.selectInputDateBean1 = sidb;
    }
    private SelectInputDateBean selectInputDate2Bean = new SelectInputDateBean();

    public SelectInputDateBean getSelectInputDate2Bean() {
        return selectInputDate2Bean;
    }

    public void setSelectInputDate2Bean(SelectInputDateBean sidb) {
        this.selectInputDate2Bean = sidb;
    }
    private PanelPopup panelPopUpLembrete = new PanelPopup();

    public PanelPopup getPanelPopUpLembrete() {
        return panelPopUpLembrete;
    }

    public void setPanelPopUpLembrete(PanelPopup pp) {
        this.panelPopUpLembrete = pp;
    }
    private HtmlInputText campoCodLembrete = new HtmlInputText();

    public HtmlInputText getCampoCodLembrete() {
        return campoCodLembrete;
    }

    public void setCampoCodLembrete(HtmlInputText hit) {
        this.campoCodLembrete = hit;
    }
    private SelectInputDate dataLembrete = new SelectInputDate();

    public SelectInputDate getDataLembrete() {
        return dataLembrete;
    }

    public void setDataLembrete(SelectInputDate sid) {
        this.dataLembrete = sid;
    }
    private HtmlInputTextarea areaDescricao = new HtmlInputTextarea();

    public HtmlInputTextarea getAreaDescricao() {
        return areaDescricao;
    }

    public void setAreaDescricao(HtmlInputTextarea hit) {
        this.areaDescricao = hit;
    }
    private SqlDateConverter sqlDateConverter2 = new SqlDateConverter();

    public SqlDateConverter getSqlDateConverter2() {
        return sqlDateConverter2;
    }

    public void setSqlDateConverter2(SqlDateConverter sdc) {
        this.sqlDateConverter2 = sdc;
    }

    // </editor-fold>

    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public Agenda() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        inicializaCampos();
        
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
            
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("Agenda Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    public List<ParcelaVendaVO> getListaDeParcelas() {
        return listaDeParcelas;
    }

    public void setListaDeParcelas(List<ParcelaVendaVO> listaDeParcelas) {
        this.listaDeParcelas = listaDeParcelas;
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    public void inicializaCampos(){
        atulizarTabelas(new Date());
        panelPopUpLembrete.setRendered(false);
    }

    public void atulizarTabelas(Date data){
        setListaDeParcelas(facadeBusca.buscarParcelaNF(data, "dataVencimento", "codParcelaVenda", ParcelaVendaVO.class));
        dataTable1Model1.setWrappedData(getListaDeParcelas());
        setListaDeLembretes(facadeBusca.buscarPorDataGeral(data, "dataLembrete", "codLembrete", LembreteVO.class));
        dataTable2Model1.setWrappedData(getListaDeLembretes());
        setListaDeContasNf(facadeBusca.buscarParcelaNF(data, "dataVencimento", "codParcelaNFEntrada", ParcelaNFEntradaVO.class));
        dataTable3Model1.setWrappedData(getListaDeContasNf());
        setListaDeContas(facadeBusca.buscarParcelaNF(data, "vencimento", "codConta", ContaVO.class));
        dataTable4Model1.setWrappedData(getListaDeContas());

    }

    public List<ContaVO> getListaDeContas() {
        return listaDeContas;
    }

    public void setListaDeContas(List<ContaVO> listaDeContas) {
        this.listaDeContas = listaDeContas;
    }

    public List<LembreteVO> getListaDeLembretes() {
        return listaDeLembretes;
    }

    public void setListaDeLembretes(List<LembreteVO> listaDeLembretes) {
        this.listaDeLembretes = listaDeLembretes;
    }
   public String btn_voltar_action() {
      //return null means stay on the same page
      return "voltar";
   }

    public String cadastra_action() {
        campoCodLembrete.setValue(facadeBusca.totalDeRegistros(LembreteVO.class)+1);
        panelPopUpLembrete.setRendered(true);
        return null;
    }

    public String btnCancelar_action() {
        panelPopUpLembrete.setRendered(false);
        return null;
    }
    
    public void limparCamposPopUp(){
        campoCodLembrete.resetValue();
        dataLembrete.resetValue();
        areaDescricao.resetValue();
    }

    public String btnCadastrar_action() {
        try {
            if (facadeCadastro.cadastrarLembrete(Integer.parseInt(campoCodLembrete.getValue().toString()), (Date) dataLembrete.getValue(), 
                    areaDescricao.getValue().toString())) {
                limparCamposPopUp();
                atulizarTabelas(new Date());
                campoDataEscolha.setValue(new Date());
                btnCancelar_action();
            }
        } catch (Exception ex) {
            System.out.println("Ocorreu umn erro: "+ex.getMessage() +" "+ ex.getStackTrace());        }
        return null;
    }

    public void campoDataEscolha_processValueChange(ValueChangeEvent vce) {
        atulizarTabelas((Date)campoDataEscolha.getValue());
    }

    public String linkContasAReceber_action() {
        return "contasAReceber";
    }

    public void button1_action() throws Exception {
        LembreteVO lembrete = (LembreteVO) dataTable2Model1.getRowData();
        facadeRemover.removerLembrete(lembrete);
        atulizarTabelas(new Date());
    }

    public String linkContasAPagarNf_action() {
        return "contasAPagar";
    }

    public String linkContas_action() {
         return "contasAPagar";
    }


}

